package com.atguigu01.collection;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class CirclePointsDemo1 extends JPanel {
    private static final int RADIUS = 300; // 设置圆的半径
    private static final int DIVISIONS = 12; // 设置等分数量

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g; // 将Graphics对象转换为Graphics2D对象
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 开启抗锯齿
        int centerX = getWidth() / 2; // 获取面板中心横坐标
        int centerY = getHeight() / 2; // 获取面板中心纵坐标
        int diameter = 2 * RADIUS; // 计算圆的直径
        int x = centerX - RADIUS; // 计算圆的左上角横坐标
        int y = centerY - RADIUS; // 计算圆的左上角纵坐标
        g2d.drawOval(x, y, diameter, diameter); // 绘制圆
        for (int i = 0; i < DIVISIONS; i++) {
            double angle1 = 2 * Math.PI * i / DIVISIONS; // 计算扇形的起始角度
            double angle2 = 2 * Math.PI * (i + 1) / DIVISIONS; // 计算扇形的终止角度
            int x1 = centerX + (int) (RADIUS * Math.cos(angle1)); // 计算扇形起始点横坐标
            int y1 = centerY + (int) (RADIUS * Math.sin(angle1)); // 计算扇形起始点纵坐标
            int x2 = centerX + (int) (RADIUS * Math.cos(angle2)); // 计算扇形终止点横坐标
            int y2 = centerY + (int) (RADIUS * Math.sin(angle2)); // 计算扇形终止点纵坐标
            g2d.drawLine(centerX, centerY, x1, y1); // 绘制分割线
        }
        // 画出同心圆
        int radius1 = 60;
        int radius2 = 120;
        int radius3 = 180;
        int radius4 = 240;

        // 画半径为60的同心圆
        int diameter1 = 2 * radius1;
        int x1 = centerX - radius1;
        int y1 = centerY - radius1;
        g2d.drawOval(x1, y1, diameter1, diameter1);

        // 画半径为120的同心圆
        int diameter2 = 2 * radius2;
        int x2 = centerX - radius2;
        int y2 = centerY - radius2;
        g2d.drawOval(x2, y2, diameter2, diameter2);

        // 画半径为180的同心圆
        int diameter3 = 2 * radius3;
        int x3 = centerX - radius3;
        int y3 = centerY - radius3;
        g2d.drawOval(x3, y3, diameter3, diameter3);

        // 画半径为240的同心圆
        int diameter4 = 2 * radius4;
        int x4 = centerX - radius4;
        int y4 = centerY - radius4;
        g2d.drawOval(x4, y4, diameter4, diameter4);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Circle Divide");
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        CirclePointsDemo1 panel = new CirclePointsDemo1();
        frame.add(panel);
        frame.setVisible(true);
    }
}